package com.workcell.workdata.im.component.listener;

import com.workcell.workdata.im.component.event.CommentEvent;
import com.workcell.workdata.im.component.event.FriendEvent;
import com.workcell.workdata.im.component.event.GroupEvent;
import com.workcell.workdata.im.component.event.MomentEvent;
import com.workcell.workdata.im.component.redis.LastMessageIdTemplate;
import com.workcell.workdata.im.constant.ChangeType;
import com.workcell.workdata.im.constant.MessageAction;
import com.workcell.workdata.im.entity.Comment;
import com.workcell.workdata.im.entity.Message;
import com.workcell.workdata.im.entity.Moment;
import com.workcell.workdata.im.service.MessageIndexService;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class EventMessageRemover {

    @Resource
    private MessageIndexService messageIndexService;

    @Resource
    private LastMessageIdTemplate lastMessageIdTemplate;

    @EventListener
    @Order(1)
    public void onGroupEvent(GroupEvent event){

        if (!event.isReplaceable()){
            return;
        }

        Message message = event.getSource();
        /*
         删除掉未接收的更新信息通知
         */
        Long lastMid = lastMessageIdTemplate.get(message.getSender(),message.getAction());
        if (lastMid != null){
            messageIndexService.delete(event.getUidList(),lastMid);
        }
        lastMessageIdTemplate.set(message.getSender(),message.getAction(),message.getId());
    }

    @EventListener
    @Order(1)
    public void onFriendEvent(FriendEvent event){
        Message message = event.getSource();
        /*
         删除掉未接收的更新信息通知
         */
        Long lastMid = lastMessageIdTemplate.get(message.getSender(),message.getAction());
        if (lastMid != null){
            messageIndexService.delete(event.getUidList(),lastMid);
        }
        lastMessageIdTemplate.set(message.getSender(),message.getAction(),message.getId());
    }

    @EventListener
    @Order(1)
    public void onMomentEvent(MomentEvent event){
        Moment moment = event.getSource();
        Message message = event.getMessage();

        if (event.getType() == ChangeType.ADD){
            lastMessageIdTemplate.set(moment.getId(),message.getAction(),message.getId());
            return;
        }

        if (event.getType() != ChangeType.DELETE){
            return;
        }


        Long lastMid = lastMessageIdTemplate.get(moment.getId(), MessageAction.ACTION_500);
        if (lastMid != null){
            messageIndexService.delete(event.getUidList(),lastMid);
        }
    }

    @EventListener
    @Order(1)
    public void onCommentEvent(CommentEvent event){
        Comment comment = event.getSource();
        Message message = event.getMessage();

        if (event.getType() == ChangeType.ADD){
            lastMessageIdTemplate.set(comment.getId(),message.getAction(),message.getId());
            return;
        }

        if (event.getType() != ChangeType.DELETE){
            return;
        }

        Long lastMid = lastMessageIdTemplate.get(comment.getId(),MessageAction.ACTION_502);
        if (lastMid != null){
            messageIndexService.delete(event.getUidList(),lastMid);
        }
    }

}
